Today, In this tutorial i will explain you step by step laravel routing tutorial. we will learn how to create new route in laravel. you can easily create post route in laravel application. i will also show how to create route in laravel controller.
Let’s see step by step process of how to create first route in laravel and understanding of laravel routing with brief.
I also posted how to install laravel, so if you don’t know how to install laravel project then follow this article Install Laravel Step By Step.
What is Laravel Routing?
Using Routing you can create a request URL for your application. you can design set of HTTP request like POST Request, GET Request, PUT Request and DELETE Request using routing in laravel.
You can easily create route in web.php file inside a routes folder. in web.php file you can create your route list there are several ways. i will show you bellow step by step how you can create it and how it works, so let’s see step by step explanation.
Create Simple Route:
Here, we will create very simple route and will let you know how you can access it. so let’s create simple route using following line adding in route file:
routes/web.php
Route::get('simple-route', function () {
return 'This is Simple Route Example of ItSolutionStuff.com';
});
Access URL:
http://localhost:8000/simple-route
Route with Call View File:
You can create route with directly call view blade file from route directly. You can see bellow created route example:
routes/web.php
Route::view('my-route', 'index');
resources/views/index.php
<h1>his is Simple Route Example of ItSolutionStuff.com</h1>
Access URL:
http://localhost:8000/my-route
Route with Controller Method:
Now, you can create route with call controller method so, you can simply create controller method and call that method with your route as like bellow:
routes/web.php
Route::get('my-route', 'TestController@index');
app/Http/Controllers/TestController.php
<?php
namespace App\Http\Controllers;
class TestController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('index');
}
}
resources/views/index.php
<h1>his is Simple Route Example of ItSolutionStuff.com</h1>
Access URL:
http://localhost:8000/my-route
Create Route with Parameter:
Here, we will create simple route with passing parameters. you can can create dynamic route with your controller. so let’s create as bellow
routes/web.php
Route::get('users/{id}', 'UserController@show');
app/Http/Controllers/UserController.php
<?php <strong>namespace</strong> App\Http\Controllers; <strong>class</strong> UserController <strong>extends</strong> Controller{ /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ <strong>public</strong> <strong>function</strong> show($id) { <strong>return</strong> 'User ID:'. $id; }}
Access URL:
http://localhost:8000/users/21
Create Route Methods:
You can create get, post, delete, put, patch methods route as bellow i created. you can understand how it works.
So, let’s see bellow route examples:
Route::get('users', 'UserController@index');
Route::post('users', 'UserController@post');
Route::put('users/{id}', 'UserController@update');
Route::delete('users/{id}', 'UserController@delete');
You can also check how many routes i created using following command:
php artisan route:list
Output like as bellow:
You can see above created routes.
You can also create resource routes as i written tutorial on it. You can see here: Create Resource Routes in Laravel.
I hope it can help you…